/*
  launchduino.c - Partial implementation of the Wiring API for the MSP430F2013.
  Part of Launchduino - http://www.launchduino.cc/

  Copyright (c) 2010 R.N

  Modified 23 January 2011 by R.N.
*/
#include "launchduino.h"

void pinMode(uint8_t pin, uint8_t mode)
{
    uint8_t digital_pins[] = {BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7};

    if (OUTPUT == mode) {
        P1DIR |= digital_pins[pin];
    } else {
        P1DIR &= ~digital_pins[pin];
    }
}

void digitalWrite(uint8_t pin, uint8_t val)
{
    uint8_t digital_pins[] = {BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7};
    
    if (HIGH == val) {
        P1OUT |= digital_pins[pin];
    } else {
        P1OUT &= ~digital_pins[pin];
    }
}

int digitalRead(uint8_t pin)
{
    uint8_t digital_pins[] = {BIT0, BIT1, BIT2, BIT3, BIT4, BIT5, BIT6, BIT7};

    if (P1IN & digital_pins[pin]) {
        return HIGH;
    } else {
        return LOW;
    }
}

void init(void)
{
    WDTCTL = WDTPW + WDTHOLD;  // Stop watchdog timer

    BCSCTL1 = CALBC1_16MHZ;
    DCOCTL = CALDCO_16MHZ;
}
